<template>
    <div class="addressAdd">

      <div >
        <van-radio-group v-model="radio" style="display: flex;padding:10px;font-size:14px;background: #fff;">
          <van-radio name="0" style="text-align: center;width:50%;">家庭住址</van-radio>
          <van-radio name="1">公司地址</van-radio>
        </van-radio-group>
        <van-address-edit
          :area-list="areaList"
          show-postal
          show-search-result
          show-set-default
          :search-result="searchResult"
          @save="onSave"
          @change-detail="onChangeDetail"
        />
      </div>


    </div>
</template>

<script>
  import areaList from './area.json';
  import { mapState } from 'vuex';
  import axios from 'axios';
    export default {
        name: "addressAdd",
      data() {
        return {
          list:'',
          areaList,
          searchResult: [],
          radio:'0',
          addressInfo:{
            username:'',
            mobile:'',
            zipcode:'',//邮编
            province_code:'',//省份代码
            city_code:'',//城市代码
            area_code:'',//区域代码
            street_code:'',//街道ID
            regions_name:'',//省/市/区文字
            address:'',//	详细地址
            isdefault:0,//是否默认
            type:0,//地址类型，0家庭，1公司
          }
        }
      },
      computed: {
        ...mapState('shop',{
          sever:'sever',
        })
      },
      methods:{
        //点击保存
        onSave(con) {
          // console.log(con)
          // console.log(con.areaCode)//110101
          this.addressInfo.username = con.name;
          this.addressInfo.mobile = con.tel;
          this.addressInfo.zipcode = con.postalCode;
          this.addressInfo.province_code = con.areaCode.substring(0,2)+'0000';
          this.addressInfo.city_code = con.areaCode.substring(0,4)+'00';
          this.addressInfo.area_code = con.areaCode;
          this.addressInfo.street_code = '0';
          this.addressInfo.regions_name = con.province+'-'+con.city+'-'+con.county;
          this.addressInfo.address = con.addressDetail;
          this.addressInfo.isDefault = con.isDefault?1:0;
          this.addressInfo.type = this.radio;
          // console.log(this.addressInfo)
          // this.$toast.success('新增成功');
          var $this = this;
          this.$http.member.post('vipaddress',{
            username:$this.addressInfo.username,
            mobile:$this.addressInfo.mobile,
            zipcode:$this.addressInfo.zipcode,
            province_code:$this.addressInfo.province_code,
            city_code:$this.addressInfo.city_code,
            area_code:$this.addressInfo.area_code,
            street_code:$this.addressInfo.street_code,
            regions_name:$this.addressInfo.regions_name,
            address:$this.addressInfo.address,
            isdefault:$this.addressInfo.isDefault,
            type:$this.addressInfo.type,
          }).then((res)=>{
            // console.log(res)
            if(!res.data.code){
              this.$toast.success('新增成功');
              var t = window.setTimeout(()=>{
                // this.$router.push({ name: "addressq"});
                this.$router.go(-1);
              },1000)
            }else{
              this.$toast.success(res.data.message);
            }
          }).catch((err)=>{
            console.log(err)
          })
        },
        // 输入详细地址
        onChangeDetail(val) {
          if (val) {
            // this.searchResult = [{
            //   name: '黄龙万科中心',
            //   address: '杭州市西湖区'
            // }];
          } else {
            this.searchResult = [];
          }
        },
        demo(){
          console.log(1)
        },
        test(){
          console.log(1)
          console.log(this.list)
        },
        //返回
        onClickLeft(){
          this.$router.go(-1);
        }
      },
      mounted(){
        //加载提示
        this.$toast.loading({
          mask: true,
          message: '加载中...',
          duration: 1000
        });

      }
    }
</script>

<style scoped>
.addressAdd-header{
  position: fixed;
  top:0;
  left:0;
  width:100%;
  z-index: 9;
  color:rgb(243, 112, 21)
}
</style>
